const Class = require('../models/classmodel')
const User = require('../models/usermodel')
const SCLtest = require('../models/SCLtestmodel')

//注册
exports.register = async (req, res) => {
  try {
    //注册开始
    var user = await User.findOne({ person_id: req.body.id });
    if (user) {
      res.status(402).json({
        message: "User name is already registered",
      });
    } else {
      var newuser = {
        person_id: req.body.id,
        password: req.body.password,
        class_id: req.body.class_id,
        name: req.body.name,
        sex: req.body.sex,
        birthday: req.body.birthday,
        identity: "0",
      };
      await User.create(newuser);
      res.status(200).json({
        message: "success",
      });
    }
  } catch (err) {
    // console.log(err);
    res.status(400).json({ message: err });
  }
};

//登录
exports.login = async (req, res) => {
  try {
    var person = await User.findOne({ person_id: req.body.id });
    // console.log(person);
    if (person == null) {
      res.status(401).json({
        message: "wrong user name",
      });
    } else {
      if (person.password != req.body.password) {
        res.status(403).json({
          message: "password err",
        });
      } else {
        res.status(200).json({
          message: "success",
          identity: person.identity,
        });
      }
    }
  } catch (err) {
    // console.log(err);
    res.status(400).json({ message: err });
  }
};

//修改密码(用户密码,用户名)
exports.change_password = async (req, res) => {
  try {
    var person = await User.findOne({ person_id: req.body.id });
    if (person.password !== req.body.password_old) {
      res.status(403).json({
        message: "wrong password",
      });
      // return res.end();
    } else {
      await User.updateOne(
        { person_id: req.body.id },
        { $set: { password: req.body.password_new } }
      );
      res.status(200).json({
        message: "success",
      });
    }
  } catch (err) {
    res.status(400).json({
      message: err,
    });
  }
};

//查找用户的个人信息
exports.person_information = async (req, res) => {
  try {
    var person = await User.findOne({ person_id: req.body.id });
    // console.log("输出个人信息：" + person);
    // console.log(req.body);
    if (person === null) {
      res.status(401).json({
        message: "not find",
      });
    } else {
      var class_name = await Class.findOne(
        { class_id: person.class_id },
        { class_name: 1 }
      );
      res.status(200).json({
        name: person.name,
        sex: person.sex,
        class: class_name,
        class_id: person.class_id,
        message: "success",
        birthday: person.birthday,
      });
    }
  } catch (err) {
    res.status(400).json({
      message: err,
    });
  }
};

//修改学生个人信息,需要文档
exports.person_update = async (req, res) => {
  try{
    var person = req.body;
    // console.log(person);
    await User.findOneAndUpdate({person_id: person.id},
      {$set:{
        name: person.name,
        sex: person.sex,
        class_id: person.class_id,
        birthday: person.birthday
      }})
      res.status(200).json({
        message: "success"
      })
  }
  catch (err) {
    res.status(400).json({
      message: err
    })
  }
}

//班级详情
exports.class_student = async (req, res) => {
  try {
    students = await User.find(
      { class_id: req.body.class_id, identity: "0" },
      { name: 1, sex: 1, person_id: 1, _id: 0 }
    );
    res.status(200).json({
      message: "success",
      students,
    });
  } catch (err) {
    res.status(400).json({
      message: err,
    });
  }
};

//删除学生，需要文档
exports.student_delete = async (req, res) => {
  try {
    await User.deleteOne({person_id: req.body.id});
    await SCLtest.deleteMany({person_id: req.body.id})
    res.status(200).json({
      message: "success",
    })
  }
  catch {
    res.status(401).json({
      message: err,
    })
  }  
  // await User.deleteOne({ person_id: req.body.id }, (err, data) => {
  //   if (err) {
  //     res.status(401).json({
  //       message: err,
  //     });
  //   }
  //   res.status(200).json({
  //     message: "success",
  //   });
  // });
};

//重置密码
exports.reset_password = async (req, res) => {
  try {
    await User.updateOne(
      { person_id: req.body.id },
      { $set: { password: "123456" } }
    );
    res.status(200).json({
      message: "success",
    });
  } catch (err) {
    res.status(400).json({
      message: err,
    });
  }
};

//批量导入学生
exports.student_import = async (req, res) => {
  try {
    var students = req.body.list;
    var class_id = req.body.class_id;
    students.forEach((student) => {
      student.password = "123456";
      student.identity = "0";
      student.class_id = class_id;
    });
    await User.insertMany(students);
    res.status(200).json({
      message: "success",
    });
  } catch (err) {
    res.status(400).json({
      message: err,
    });
  }
};

//创建单个学生
exports.student_create = async (req, res) => {
  try {
    var user = await User.findOne({ person_id: req.body.id });
    if (user) {
      res.status(402).json({
        message: "User name is already registered",
      });
    } else {
      var newuser = {
        person_id: req.body.id,
        password: "123456",
        class_id: req.body.class_id,
        name: req.body.name,
        sex: req.body.sex,
        birthday: req.body.birthday,
        identity: "0",
      };
      await User.create(newuser);
      res.status(200).json({
        message: "success",
      });
    }
  } catch (err) {
    res.status(400).json({
      message: err,
    });
  }
};

//查看所有老师
exports.teacher_view = async (req, res) => {
  try {
    teachers = await User.find(
      { identity: "1" },
      { name: 1, sex: 1, person_id: 1, _id: 0 }
    );
    res.status(200).json({
      message: "success",
      teachers,
    });
  } catch (err) {
    res.status(400).json({
      message: err,
    });
  }
};

//创建老师
exports.teacher_create = async (req, res) => {
  try {
    var user = await User.findOne({ person_id: req.body.id });
    if (user) {
      res.status(402).json({
        message: "User name is already registered",
      });
    } else {
      var newuser = {
        person_id: req.body.id,
        password: "123456",
        class_id: req.body.class_id,
        name: req.body.name,
        sex: req.body.sex,
        birthday: req.body.birthday,
        identity: "1",
      };
      await User.create(newuser);
      res.status(200).json({
        message: "success",
      });
    }
  } catch (err) {
    res.status(400).json({
      message: err,
    });
  }
};

//删除老师
exports.teacher_delete = async (req, res) => {
  await User.deleteOne({ person_id: req.body.id }, (err, data) => {
    if (err) {
      res.status(401).json({
        message: err,
      });
    }
    res.status(200).json({
      message: "success",
    });
  });
};
